const OpenAI = require("openai");
const express = require("express");
const app = express();
// 接收post请求的参数
app.use(express.json());
// 解决跨域
app.use(require("cors")());

app.post("/", async (req, res) => {
  //  获取前端传递过来的post请求的参数
  const { content } = req.body;

  // 构造 client
  const client = new OpenAI({
    // apiKey: process.env["HUNYUAN_API_KEY"], // 混元 APIKey
    apiKey: "sk-", // 混元 APIKey
    baseURL: "https://api.hunyuan.cloud.tencent.com/v1", // 混元 endpoint
  });

  // 自定义参数传参示例
  const completion = await client.chat.completions.create({
    model: "hunyuan-pro",
    messages: [{ role: "user", content: content }],
    // @ts-expect-error key is not yet public
    enable_enhancement: true, // <- 自定义参数
  });

  res.send(completion.choices[0]?.message?.content);
});
app.listen(3001, () => {
  console.log("http://localhost:3001"); // 服务启动后输出
});
